package com.chang.rentmall.servicesImpl;

import com.chang.rentmall.beans.GoodsBean;
import com.chang.rentmall.daos.GoodsDao;
import com.chang.rentmall.forms.AddGoodsFormBean;
import com.chang.rentmall.services.GoodsServices;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsServiceImpl implements GoodsServices {
    @Autowired
    GoodsDao goodsDao;
    @Override
    public HashMap<String, Object> addGoods(GoodsBean goodsBean) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        goodsBean.setCreateTime(df.format(new Date()));
        //生成32位字符串小写--商品ID
        goodsBean.setGoodsId(getRandomLowerLetter());
        int flag = goodsDao.addGoods(goodsBean);
        if (flag == 1) {
            result.put("code", 200);
            result.put("message", "操作成功");
            result.put("body", "");
        } else {
            result.put("code", -1);
            result.put("message", "操作失败");
            result.put("body", "");
        }
        return result;
    }

	@Override
    public HashMap<String, Object> updateGoods(GoodsBean goodsBean) {
    	HashMap<String, Object> result = new HashMap<String, Object>();
        int flag = goodsDao.updateGoods(goodsBean);
        if (flag == 1) {
            result.put("code", 200);
            result.put("body", "");
            result.put("message", "操作成功");

        } else {
            result.put("code", 200);
            result.put("body", "");
            result.put("message", "操作成功");
        }
        return result;
    }

    @Override
    public HashMap<String, Object> getGoodsByShop(AddGoodsFormBean addGoodsFormBean) {
    	HashMap<String, Object> result = new HashMap<String, Object>();
        List<GoodsBean> list = goodsDao.getGoodsByShop(addGoodsFormBean.getQuery(), addGoodsFormBean.getPagination());
        result.put("code", 200);
        result.put("message", "查询数据成功");
        result.put("list", list);
        return result;
    }
    
    @Override
    public HashMap<String, Object> getGoodsById(int id) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        GoodsBean goodsBean = goodsDao.getGoodsById(id);
        result.put("message", "查询成功");
        result.put("code", 200);
        result.put("list", goodsBean);
        return result;
    }
    
    @Override
    public HashMap<String, Object> updateGoodsStatus(AddGoodsFormBean addGoodsFormBean) {
    	HashMap<String, Object> result = new HashMap<String, Object>();
        int flag = goodsDao.updateGoodsStatus(addGoodsFormBean.getQuery());
        if (flag == 1) {
            result.put("code", 200);
            result.put("body", "");
            result.put("message", "操作成功");

        } else {
            result.put("code", 200);
            result.put("body", "");
            result.put("message", "操作成功");
        }
        return result;
    }
    
    //生成随机字符串
  	public static String getRandomLowerLetter() {
  		String ch = "";
  		for(int i=0;i<32;i++) {
  			if(((int)(Math.random()*10)+1)%2 == 0) {
  				ch += (int)(Math.random()*9)+1;
  			}else {
  				ch += (char)('a' + Math.random() * ('z' - 'a' + 1));
  			}
  		}
  		return ch;
  	}
}
